home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / ISp Sample / Source / EventHandler.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  8.1 KB  |  411 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        EventHandler.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                xxx put dri here xxx
  13.  
  14.         Other Contact:        xxx put other contact here xxx
  15.  
  16.         Technology:            xxx put technology here xxx
  17.  
  18.     Writers:
  19.  
  20.         (BWS)    Brent Schorsch
  21.  
  22.     Change History (most recent first):
  23.  
  24.        <SP1>      7/1/99    BWS        first checked in
  25. */
  26.  
  27. //•    ————————————————————————————————————————    Includes
  28.  
  29. #include <DiskInit.h>
  30. #include <Menus.h>
  31. #include <ToolUtils.h>
  32. #include <Windows.h>
  33.  
  34. #include "AppleEventHandler.h"
  35. #include "EventHandler.h"
  36. #include "MainWindow.h"
  37. #include "MenuHandler.h"
  38. #include "ShellWindow.h"
  39.  
  40. //•    ————————————————————————————————————————    Private Definitions
  41. //•    ————————————————————————————————————————    Private Types
  42. //•    ————————————————————————————————————————    Private Variables
  43.  
  44. static SInt16    gSleepTime;
  45.  
  46. //•    ————————————————————————————————————————    Private Functions
  47.  
  48. static void EventDispatch(const EventRecord *inEvent);
  49. static Boolean DefaultMouseDown(const EventRecord *inEvent);
  50. static Boolean DefaultKeyDown(const EventRecord *inEvent);
  51. static Boolean DoDiskEvent(const EventRecord *inEvent);
  52. static void DoSuspend(void);
  53.  
  54. //•    ————————————————————————————————————————    Public Variables
  55.  
  56. Boolean                gDone = false;
  57. EventHandlerSet        gEventHandlers;
  58.  
  59. //•    ————————————————————    EventInit
  60.  
  61. short
  62. EventInit(void)
  63. {
  64. SInt16        modifiers = 0;
  65. EventRecord    dummy;
  66.  
  67.     EventAvail(everyEvent, &dummy);
  68.     modifiers |= dummy.modifiers;
  69.     EventAvail(everyEvent, &dummy);
  70.     modifiers |= dummy.modifiers;
  71.     EventAvail(everyEvent, &dummy);
  72.     modifiers |= dummy.modifiers;
  73.  
  74.     RegisterEventHandlers(nil, nil, nil, nil, nil);
  75.     gEventHandlers.diskHandler = DoDiskEvent;
  76.  
  77.     gSleepTime = 32767;
  78.     
  79.     AppleEventsRegisterQuitFlag(&gDone);
  80.  
  81.     new MainWindow;
  82.  
  83.     return (modifiers);
  84. }
  85.  
  86. //•    ————————————————————    EventLoop
  87.  
  88. void
  89. EventLoop(void)
  90. {
  91. EventRecord    theEvent;
  92.  
  93.     while (false == gDone)
  94.     {
  95.         WaitNextEvent(everyEvent, &theEvent, gSleepTime, nil);
  96.         EventDispatch(&theEvent);
  97.     }
  98. }
  99.  
  100. //•    ————————————————————    EventDispatch
  101.  
  102. static void
  103. EventDispatch(const EventRecord *inEvent)
  104. {
  105. WindowPtr    theWindow;
  106.  
  107.     switch (inEvent->what)
  108.     {
  109.         case nullEvent:
  110.             //•    Run any idle tasks
  111.             if (gEventHandlers.idleHandler)
  112.                 (*gEventHandlers.idleHandler)(inEvent);
  113.  
  114.             theWindow = FrontWindow();
  115.             if (nil != theWindow)
  116.             {
  117.                 if (userKind == GetWindowKind(theWindow))
  118.                 {
  119.                 ShellWindow    *myWindow;
  120.                 
  121.                     myWindow = (ShellWindow *) GetWRefCon(theWindow);
  122.                     if (nil != myWindow)
  123.                         myWindow->Idle();
  124.                 }
  125.             }
  126.             break;
  127.  
  128.         case mouseDown:
  129.             if (gEventHandlers.clickHandler != nil)
  130.                 if ((*gEventHandlers.clickHandler)(inEvent))
  131.                 {
  132.                     break;
  133.                 }
  134.  
  135.             DefaultMouseDown(inEvent);
  136.             break;
  137.  
  138.         case keyDown:
  139.             if (gEventHandlers.keyHandler != nil)
  140.                 if ((*gEventHandlers.keyHandler)(inEvent))
  141.                 {
  142.                     break;
  143.                 }
  144.  
  145.             DefaultKeyDown(inEvent);
  146.             break;
  147.  
  148.         case autoKey:
  149.             if (gEventHandlers.autoKeyHandler != nil)
  150.                 if ((*gEventHandlers.autoKeyHandler)(inEvent))
  151.                 {
  152.                     break;
  153.                 }
  154.  
  155.             DefaultKeyDown(inEvent);
  156.             break;
  157.  
  158.         case activateEvt:
  159.         {
  160.         WindowPtr    theWindow;
  161.         
  162.             theWindow = (WindowPtr) inEvent->message;
  163.             if (userKind == GetWindowKind(theWindow))
  164.             {
  165.             ShellWindow    *myWindow;
  166.             
  167.                 myWindow = (ShellWindow *) GetWRefCon(theWindow);
  168.                 
  169.                 if (nil != myWindow)
  170.                     myWindow->Activate((inEvent->modifiers & activeFlag) ? true : false);
  171.             }
  172.         }
  173.         break;
  174.  
  175.         case updateEvt:
  176.         {
  177.         WindowPtr    theWindow;
  178.         
  179.             theWindow = (WindowPtr) inEvent->message;
  180.             if (userKind == GetWindowKind(theWindow))
  181.             {
  182.             ShellWindow    *myWindow;
  183.             
  184.                 myWindow = (ShellWindow *) GetWRefCon(theWindow);
  185.                 if (nil != myWindow)
  186.                     myWindow->Update();
  187.             }
  188.         }
  189.         break;
  190.  
  191.         case diskEvt:
  192.             if (gEventHandlers.diskHandler != nil)
  193.                 (*gEventHandlers.diskHandler)(inEvent);
  194.             break;
  195.  
  196.         case osEvt:
  197.             if (inEvent->message & 0x01000000)        //•    Suspend/resume event
  198.             {
  199.                 ShellWindow    * myWindow = nil;
  200.                 
  201.                 theWindow = FrontWindow();
  202.                 if (nil != theWindow)
  203.                 {
  204.                     if (userKind == GetWindowKind(theWindow))
  205.                     {
  206.                         myWindow = (ShellWindow *) GetWRefCon(theWindow);
  207.                     }
  208.                 }
  209.  
  210.  
  211.                 if (inEvent->message & 0x00000001)    //•    Resume
  212.                 {
  213.                     if (nil != myWindow)
  214.                         myWindow->Resume();
  215.                 }
  216.                 else
  217.                 {
  218.                     if (nil != myWindow)
  219.                         myWindow->Suspend();
  220.                     //DoSuspend();                    //•    Suspend
  221.                 }
  222.             }
  223.             break;
  224.  
  225.         case kHighLevelEvent:
  226.             AEProcessAppleEvent(inEvent);
  227.             break;
  228.     }
  229. }
  230.  
  231. //•    ————————————————————————————————————————    DefaultMouseDown
  232.  
  233. static Boolean
  234. DefaultMouseDown(const EventRecord *inEvent)
  235. {
  236. SInt16                whatPart;
  237. SInt32                menuResult;
  238. WindowPtr            whichWindow;
  239. ShellWindow            *myWindow = nil;
  240.  
  241.     whatPart = FindWindow(inEvent->where, &whichWindow);
  242.     
  243.     if (userKind == GetWindowKind(whichWindow))
  244.         myWindow = (ShellWindow *) GetWRefCon(whichWindow);
  245.  
  246.     switch (whatPart)
  247.     {
  248.         case inDrag:
  249.             if (nil != myWindow)
  250.                 myWindow->Drag(inEvent->where, inEvent->modifiers);
  251.             break;
  252.             
  253.         case inGoAway:
  254.             if (TrackGoAway(whichWindow, inEvent->where))
  255.             {
  256.                 if (nil != myWindow)
  257.                 {
  258.                     myWindow->Close();
  259.                     delete myWindow;
  260.                     gDone = true;
  261.                 }
  262.             }
  263.             break;
  264.  
  265.         case inZoomIn:
  266.         case inZoomOut:
  267.             if (TrackBox(whichWindow, inEvent->where, whatPart))
  268.             {
  269.                 if (nil != myWindow)
  270.                     myWindow->Zoom(whatPart);
  271.             }
  272.             break;
  273.             
  274.         case inGrow:
  275.             if (nil != myWindow)
  276.                 myWindow->Grow(inEvent->where);
  277.             break;
  278.  
  279.         case inContent:
  280.             if (nil != myWindow)
  281.                 myWindow->Click(inEvent->where, inEvent->modifiers);
  282.             break;
  283.  
  284.         case inMenuBar:
  285.             MenuPrepForSelect();
  286.             menuResult = MenuSelect(inEvent->where);
  287.  
  288.             if (HiWord(menuResult) != 0)
  289.                 MenuDispatch(menuResult, inEvent->modifiers);
  290.             break;
  291.  
  292.         case inSysWindow:
  293.             SystemClick(inEvent, whichWindow);
  294.             break;
  295.     }
  296.  
  297.     return (true);
  298. }
  299.  
  300. //•    ————————————————————————————————————————    DefaultKeyDown
  301.  
  302. static Boolean
  303. DefaultKeyDown(const EventRecord *inEvent)
  304. {
  305. SInt8                theKey;
  306. SInt8                theCode;
  307. SInt32                menuResult;
  308. WindowPtr            whichWindow;
  309. ShellWindow            *myWindow = nil;
  310.  
  311.     whichWindow = FrontWindow();
  312.  
  313.     if (userKind == GetWindowKind(whichWindow))
  314.         myWindow = (ShellWindow *) GetWRefCon(whichWindow);
  315.         
  316.     theKey = inEvent->message & charCodeMask;
  317.     theCode = (inEvent->message & keyCodeMask) >> 8;
  318.  
  319.     if ((inEvent->modifiers & cmdKey) != 0)
  320.     {
  321.         MenuPrepForSelect();
  322.         
  323.         menuResult = MenuKey(theKey);
  324.         if (HiWord(menuResult) != 0)
  325.             if (MenuDispatch(menuResult, inEvent->modifiers))
  326.                 return (true);
  327.     }
  328.  
  329.     if (nil != myWindow)
  330.         return (myWindow->Key(theKey, theCode, inEvent->modifiers));
  331.     
  332.     return (false);
  333. }
  334.  
  335. //•    ————————————————————————————————————————    DoDiskEvent
  336.  
  337. static Boolean
  338. DoDiskEvent(const EventRecord *inEvent)
  339. {
  340. Point    thePoint;
  341. SInt16    value;
  342.  
  343.     if (inEvent->message & 0xFFFF0000)        //•    Error mounting disk
  344.     {
  345.         SetPt(&thePoint, 100, 100);
  346.         value = DIBadMount(thePoint, inEvent->message);
  347.     }
  348.  
  349.     return (true);
  350. }
  351.  
  352. //•    ————————————————————————————————————————    RegisterEventHandlers
  353.  
  354. void
  355. RegisterEventHandlers(
  356.     EventHandlerProc inKeyDown,
  357.     EventHandlerProc inAutoKey,
  358.     EventHandlerProc inMouseDown,
  359.     EventHandlerProc inUpdate,
  360.     EventHandlerProc inIdle)
  361. {
  362.     gEventHandlers.keyHandler = inKeyDown;
  363.     gEventHandlers.autoKeyHandler = inAutoKey;
  364.     gEventHandlers.clickHandler = inMouseDown;
  365.     gEventHandlers.updateHandler = inUpdate;
  366.     gEventHandlers.idleHandler = inIdle;
  367. }
  368.  
  369. //•    ————————————————————————————————————————    ModifyEventHandlers
  370.  
  371. void
  372. ModifyEventHandlers(
  373.     EventHandlerProc inKeyDown,
  374.     EventHandlerProc inAutoKey,
  375.     EventHandlerProc inMouseDown,
  376.     EventHandlerProc inUpdate,
  377.     EventHandlerProc inIdle)
  378. {
  379.     if (inKeyDown)
  380.         gEventHandlers.keyHandler = inKeyDown;
  381.  
  382.     if (inAutoKey)
  383.         gEventHandlers.autoKeyHandler = inAutoKey;
  384.  
  385.     if (inMouseDown)
  386.         gEventHandlers.clickHandler = inMouseDown;
  387.  
  388.     if (inUpdate)
  389.         gEventHandlers.updateHandler = inUpdate;
  390.  
  391.     if (inIdle)
  392.         gEventHandlers.idleHandler = inIdle;
  393. }
  394.  
  395. //•    ————————————————————    DoSuspend
  396.  
  397. static void
  398. DoSuspend(void)
  399. {
  400. EventRecord    theEvent;
  401.  
  402.     while (true)
  403.     {
  404.         //•    Only check for resume and update events.  While doing so, give TONS of time to other apps
  405.         if (WaitNextEvent(osMask, &theEvent, 32767, nil))
  406.             if (theEvent.message & 0x01000000)        //•    Suspend/resume event
  407.                 if (theEvent.message & 0x00000001)    //•    Resume
  408.                     break;
  409.     }
  410. }
  411.